﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cwtrock.model;
using System.Data.SqlClient;
using System.Data;
using cwtrock.util;

namespace cwtrock.dao.impl
{
    class SysUsrDaoImpl:BasicDaoImpl
    {
     
        private GetConn getConn = new GetConn();

        public SysUsr getUsrByIdAndPwd(SysUsr usr)
        {

            try
            {
                
                StringBuilder strSql = new StringBuilder();
                strSql.Append(" SELECT    TOP 1    ID, usrid, usrname,usrsex,usremail,registerdate ,usrstat,usrpwd  ");
                strSql.Append("   FROM SYS_USERS WHERE  usrid=@usrid AND usrpwd=@usrpwd ");


                SqlParameter[] parameters = { 
                new SqlParameter("@usrid",SqlDbType.VarChar,50),
                new SqlParameter("@usrpwd",SqlDbType.VarChar,50)
                                        };

            
                    parameters[0].Value = usr.Usrid;
                    parameters[1].Value = usr.Usrpwd;


                    SqlCommand cmd = new SqlCommand(strSql.ToString(), getConn.GetCon());
                    cmd.Parameters.Add(parameters[0]);
                    cmd.Parameters.Add(parameters[1]);
                    cmd.ExecuteNonQuery();

                    SqlDataReader reader = cmd.ExecuteReader();   
                    SysUsr rtnuser = new SysUsr();

                   if (reader.Read())
                    {
                      string usrid = reader[1].ToString();
                      string   usrname = reader[2].ToString();
                      rtnuser.Usrname = usrname;
                      rtnuser.Usrid = usrid;
                    }

                   return rtnuser;
                
               

            }
            catch (Exception e)
            {
                throw e;
                return null;
            }
        }


        public void updatePwd(string userid, string pwd,string usrname)
        {
            string sql = "update SYS_USERS set usrpwd=@usrpwd,usrname=@usrname where usrid=@usrid";
            SqlCommand cmd = new SqlCommand(sql, getConn.GetCon());

            SqlParameter[] parameters = {
                new SqlParameter("@usrid",SqlDbType.VarChar,50),
                new SqlParameter("@usrpwd",SqlDbType.VarChar,50),
                 new SqlParameter("@usrname",SqlDbType.VarChar,50)
            };

            parameters[0].Value = userid;
            parameters[1].Value = pwd;
            parameters[2].Value = usrname;

            cmd.Parameters.Add(parameters[0]);
            cmd.Parameters.Add(parameters[1]);
            cmd.Parameters.Add(parameters[2]);
            cmd.ExecuteNonQuery();

            AppContextProvider.instance.LogUser.Usrpwd = pwd;
            AppContextProvider.instance.LogUser.Usrname = usrname;

        }


        public SysUsr getUserByUserid(string usrid)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append(" SELECT    TOP 1    ID, usrid, usrname,usrsex,usremail,registerdate ,usrstat,usrpwd  ");
                strSql.Append("   FROM SYS_USERS WHERE  usrid=@usrid");


                SqlParameter[] parameters = { 
                new SqlParameter("@usrid",SqlDbType.VarChar,50),
                };
                    parameters[0].Value = usrid;


                    SqlCommand cmd = new SqlCommand(strSql.ToString(), getConn.GetCon());
                    cmd.Parameters.Add(parameters[0]);
                    cmd.ExecuteNonQuery();

                    SqlDataReader reader = cmd.ExecuteReader();
                    SysUsr usr = new SysUsr();
                    if (reader.Read())
                    {
                        usr.Id = long.Parse(reader[0].ToString());
                        usr.Usrid = reader[1].ToString();
                        usr.Usrname = reader[2].ToString();
                        usr.Usrpwd = reader[7].ToString();
                    }

                    return usr;
            }
            catch (Exception e)
            {
                throw e;
                return null;
            }

        }

    }
}
